/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerSyncDialog.java
 *
 * Created on Dec 11, 2009, 5:30:49 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.musicjukebox.ui.components.MusicJDialog;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.bjj.utils.Resources.LABEL_KEY;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import org.bff.slimserver.SlimPlayer;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.exception.SlimPlayerException;

/**
 *
 * @author bill
 */
public class PlayerSyncDialog extends MusicJDialog {

    private SlimPlayer player;
    private DefaultComboBoxModel syncModel;

    /** Creates new form PlayerSyncDialog */
    public PlayerSyncDialog(java.awt.Frame parent, boolean modal, SlimPlayer player) {
        super(parent, modal);
        this.player = player;

        loadSyncGroups();

        initComponents();
        try {
            lblTitle.setText(getResource(LABEL_KEY.PLAYER_SYNC_TITLE, new Object[]{player.getName()}));
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerSyncDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new BJJPanel();
        cbSync = new javax.swing.JComboBox();
        lblTitle = new javax.swing.JLabel();
        btnSync = new BJJButton();
        btnCancel = new BJJButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Synchronize Player");
        setMinimumSize(new java.awt.Dimension(300, 200));

        jPanel1.setLayout(new java.awt.GridBagLayout());

        cbSync.setModel(syncModel);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(cbSync, gridBagConstraints);

        lblTitle.setText("jLabel1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lblTitle, gridBagConstraints);

        btnSync.setText("Synchronize");
        btnSync.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSyncActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(btnSync, gridBagConstraints);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(btnCancel, gridBagConstraints);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSyncActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSyncActionPerformed
        try {
            if (cbSync.getSelectedItem() instanceof SlimPlayer) {
                getPlayer().syncPlayer((SlimPlayer) cbSync.getSelectedItem());
            } else {
                getPlayer().syncPlayer(new ArrayList<SlimPlayer>((Collection<SlimPlayer>) cbSync.getSelectedItem()).get(0));
            }
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerSyncDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SlimPlayerException ex) {
            Logger.getLogger(PlayerSyncDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        dispose();
    }//GEN-LAST:event_btnSyncActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                PlayerSyncDialog dialog = new PlayerSyncDialog(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSync;
    private javax.swing.JComboBox cbSync;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblTitle;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the player
     */
    public SlimPlayer getPlayer() {
        return player;
    }

    /**
     * @param player the player to set
     */
    public void setPlayer(SlimPlayer player) {
        this.player = player;
    }

    private void loadSyncGroups() {
        syncModel = new DefaultComboBoxModel();
        try {
            List<SlimPlayer> groupList = new ArrayList<SlimPlayer>(getPlayer().getSlimServer().getPlayerSyncGroups());
            if (groupList.size() > 0) {
                syncModel.addElement(groupList);
            }

            for (SlimPlayer p : getPlayer().getSlimServer().getSlimPlayers()) {
                if (!p.equals(getPlayer()) && !groupList.contains(p)) {
                    syncModel.addElement(p);
                }
            }
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
